package com.happy.unitconvert.common.result;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.time.LocalDateTime;

/**
 * 统一API响应结果封装
 * 
 * @param <T> 数据类型
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {

  /**
   * 响应码
   */
  private Integer code;

  /**
   * 响应消息
   */
  private String message;

  /**
   * 响应数据
   */
  private T data;

  /**
   * 响应时间戳
   */
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime timestamp;

  /**
   * 请求ID（用于链路追踪）
   */
  private String requestId;

  public Result() {
    this.timestamp = LocalDateTime.now();
  }

  public Result(Integer code, String message) {
    this();
    this.code = code;
    this.message = message;
  }

  public Result(Integer code, String message, T data) {
    this(code, message);
    this.data = data;
  }

  /**
   * 成功响应
   */
  public static <T> Result<T> success() {
    return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
  }

  /**
   * 成功响应带数据
   */
  public static <T> Result<T> success(T data) {
    return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
  }

  /**
   * 成功响应带消息和数据
   */
  public static <T> Result<T> success(String message, T data) {
    return new Result<>(ResultCode.SUCCESS.getCode(), message, data);
  }

  /**
   * 失败响应
   */
  public static <T> Result<T> error() {
    return new Result<>(ResultCode.INTERNAL_SERVER_ERROR.getCode(), ResultCode.INTERNAL_SERVER_ERROR.getMessage());
  }

  /**
   * 失败响应带消息
   */
  public static <T> Result<T> error(String message) {
    return new Result<>(ResultCode.INTERNAL_SERVER_ERROR.getCode(), message);
  }

  /**
   * 失败响应带错误码
   */
  public static <T> Result<T> error(ResultCode resultCode) {
    return new Result<>(resultCode.getCode(), resultCode.getMessage());
  }

  /**
   * 失败响应带错误码和数据
   */
  public static <T> Result<T> error(ResultCode resultCode, T data) {
    return new Result<>(resultCode.getCode(), resultCode.getMessage(), data);
  }

  /**
   * 失败响应带自定义错误码、消息和数据
   */
  public static <T> Result<T> error(Integer code, String message, T data) {
    return new Result<>(code, message, data);
  }

  /**
   * 自定义响应
   */
  public static <T> Result<T> result(Integer code, String message, T data) {
    return new Result<>(code, message, data);
  }

  // Getters and Setters
  public Integer getCode() {
    return code;
  }

  public void setCode(Integer code) {
    this.code = code;
  }

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  public LocalDateTime getTimestamp() {
    return timestamp;
  }

  public void setTimestamp(LocalDateTime timestamp) {
    this.timestamp = timestamp;
  }

  public String getRequestId() {
    return requestId;
  }

  public void setRequestId(String requestId) {
    this.requestId = requestId;
  }

  /**
   * 判断是否成功
   */
  public boolean isSuccess() {
    return ResultCode.SUCCESS.getCode().equals(this.code);
  }

  @Override
  public String toString() {
    return "Result{" +
        "code=" + code +
        ", message='" + message + '\'' +
        ", data=" + data +
        ", timestamp=" + timestamp +
        ", requestId='" + requestId + '\'' +
        '}';
  }
}
